package com.zhou.service.impl;

import com.zhou.service.WeChatInterfaceService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.draft.WxMpAddDraft;
import me.chanjar.weixin.mp.bean.draft.WxMpDraftArticles;
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lcz
 * @desc 微信接口服务
 * @mail lcz_0130@163.com
 * @date 2023/12/29
 */
@Service
@Slf4j
public class WeChatInterfaceServiceImpl implements WeChatInterfaceService {
    @Resource
    private WxMpService wxMpService;

    /**
     * 上传永久素材（占用容量）
     *
     * @param image 图片本地地址
     * @return {@link String}
     */
    @Override
    public String imgId(String image) {
        //永久图片 ID
        WxMpMaterial wxMpMaterial = new WxMpMaterial("img", new File(image), "图片", "图片");
        WxMpMaterialUploadResult wxMediaImgUploadResult = null;
        try {
            wxMediaImgUploadResult = wxMpService.getMaterialService().materialFileUpload(WxConsts.MediaFileType.IMAGE, wxMpMaterial);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return wxMediaImgUploadResult.getMediaId();

    }

    /**
     * 永久图片链接（不占用容量）
     *
     * @param image 图片本地地址
     * @return {@link String}
     */
    @Override
    public String imgUrl(String image) {
        //永久图片链接
        WxMediaImgUploadResult wxMediaImgUploadResult = null;
        try {
            wxMediaImgUploadResult = wxMpService.getMaterialService().mediaImgUpload(new File(image));
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return wxMediaImgUploadResult.getUrl();
    }

    /**
     * 新建草稿
     *
     * @param imgMediaId 图片id
     * @param title      标题
     * @param content    文章内容
     * @return {@link String}
     */
    @Override
    public String newDraft(String imgMediaId, String title, String content) {
        WxMpAddDraft wxMpAddDraft = new WxMpAddDraft();
        List<WxMpDraftArticles> wxMpDraftArticlesList = new ArrayList<>();
        WxMpDraftArticles articles = new WxMpDraftArticles();
        articles.setTitle(title);
//        articles.setContent(String.format("正文<div><img src=\"%s\"> </div>", imgUrl));
        articles.setContent(content);
        articles.setThumbMediaId(imgMediaId);
        wxMpDraftArticlesList.add(articles);
        wxMpAddDraft.setArticles(wxMpDraftArticlesList);
        WxMpAddDraft draftFormat = WxMpAddDraft.fromJson(wxMpAddDraft.toJson());

        String mediaId = null;
        try {
            mediaId = wxMpService.getDraftService().addDraft(draftFormat);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return mediaId;
    }

    /**
     * 提交/发布草稿
     *
     * @param mediaId 草稿id
     * @return {@link String}
     */
    @Override
    public String submit(String mediaId) {
        String submit = null;
        try {
            submit = wxMpService.getFreePublishService().submit(mediaId);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return submit;
    }
}
